package com.xteam.ecsystem.directive;

import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

import java.io.IOException;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xteam.ecsystem.base.Constant;
import com.xteam.ecsystem.service.front.FrontGoodsInfoService;
import com.xteam.ecsystem.utils.DirectiveUtil;
import com.xteam.ecsystem.utils.PageCenterStation;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 相关商品标签
 * @author ls
 */
@Service
public class RelatedGoodsDirective implements TemplateDirectiveModel {
	
	@Autowired
	private FrontGoodsInfoService fgiService;

	@SuppressWarnings("unchecked")
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		String goodsId = DirectiveUtil.getString(params, Constant.PAR_GOODSID.value());
		int pageIndex = DirectiveUtil.getInteger(params, Constant.PAR_PAGEINDEX.value());
		int pageSize = DirectiveUtil.getInteger(params, Constant.PAR_PAGESIZE.value());
		PageCenterStation<?> p=this.fgiService.getRelatedGoods(goodsId,pageIndex,pageSize);
		env.setVariable(Constant.VAR_RELATEDGOODSINFO.value(),DEFAULT_WRAPPER.wrap(p));
		body.render(env.getOut());
	}
}